package com.aiyige.utils;

import com.aiyige.model.Divide;
import com.alibaba.fastjson.JSON;

public class DivideUtil {
    public static final long LEARN_VIDEO_DIVIDE_EXPIRE_TIME_MS = 24 * 60 * 60 * 1000;
    public static final long MAJOR_COURSE_DIVIDE_EXPIRE_TIME_MS = 24 * 60 * 60 * 1000;
    public static final String PREF_KEY_LEARN_VIDEO_DIVIDE = "com.aiyige.pref.PREF_KEY_LEARN_VIDEO_DIVIDE";
    public static final String PREF_KEY_MAJOR_COURSE_DIVIDE = "com.aiyige.pref.PREF_KEY_MAJOR_COURSE_DIVIDE";

    public static void putLearnVideoDivide(Divide divide) {
        if (divide == null) return;
        PreferenceUtil.putString(PREF_KEY_LEARN_VIDEO_DIVIDE, JSON.toJSONString(divide));
    }

    public static void putMajorCourseDivide(Divide divide) {
        if (divide == null) return;
        PreferenceUtil.putString(PREF_KEY_MAJOR_COURSE_DIVIDE, JSON.toJSONString(divide));
    }

    public static Divide getLearnVideoDivide() {
        String json = PreferenceUtil.getString(PREF_KEY_LEARN_VIDEO_DIVIDE, "");
        try {
            Divide divide = JSON.parseObject(json, Divide.class);
            if (System.currentTimeMillis() - divide.getTimestamp() > LEARN_VIDEO_DIVIDE_EXPIRE_TIME_MS) {
                return null;
            }
            return divide;
        } catch (Exception e) {
            return null;
        }
    }

    public static Divide getMajorCourseDivide() {
        String json = PreferenceUtil.getString(PREF_KEY_MAJOR_COURSE_DIVIDE, "");
        try {
            Divide divide = JSON.parseObject(json, Divide.class);
            if (System.currentTimeMillis() - divide.getTimestamp() > MAJOR_COURSE_DIVIDE_EXPIRE_TIME_MS) {
                return null;
            }
            return divide;
        } catch (Exception e) {
            return null;
        }
    }
}
